import _ from 'lodash'
import iaxios from './iaxios'

const defaultPagination = {
    current: 1,
    size: 10
}

const paginationBody = (body, pagination) => {
    let _body = _.cloneDeep(body || {})
    pagination = _.defaultsDeep(pagination, defaultPagination);
    let _pag = {
        sort: pagination.sort || null,   // 排序字段, 可多个字段
        order: pagination.order || null, // ASC|DESC
        pageOffset: pagination.pageOffset || pagination.current || 1,
        pageSize: pagination.pageSize || pagination.size || 10
    }
    return _.defaultsDeep(_body, _pag)
}

export default {
    defaultPagination,
    paginationBody,
    get (url, body, options) {
        return iaxios.get(url, options)
    },
    post (url, body, options) {
        return iaxios.post(url, body, options)
    },
    basePath () {
        return iaxios.basePath()
    },
    redirect (url) {
        return iaxios.redirect(url)
    },
    open (url) {
        return iaxios.open(url)
    },
    upload(url, file, body, options) {
        const formData = new FormData()
        formData.append('file', file)
        if (!_.isEmpty(body)) {
            _.forEach(body, (val, key) => formData.append(key, val))
        }
        return iaxios.post(url, formData, options)
    },
    download: function (url, options) {
        return iaxios.download(url, options)
    }
}
